package com.fire.control.system.support.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageInfo;
/**
 * 
 * @ClassName:  ResultPageUtil
 * @Description:  针对dto分页查询返回设置工具类
 * @author:  xiechengguang
 * @date:  2017年5月18日 下午4:30:46
 *
 */
@Component
public class ResultPageUtil {
	
	public final static Map<String, Object> resultPage(Page<?> page, List<?> dtoList) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("content", dtoList);
		result.put("last", page.isLast());
		result.put("totalElements", page.getTotalElements());
		result.put("totalPages", page.getTotalPages());
		result.put("number", page.getNumber());
		result.put("size", page.getSize());
		result.put("sort", page.getSort());
		result.put("numberOfElements", page.getNumberOfElements());
		result.put("first", page.isFirst());
		return result;
	}
	
	
	/**
	* @Title: resultPage
	* @Description: TODO(web端-mybatis分页兼容之前的jpa分页)
	* @author wanbo
	* @date  2017年6月8日 上午11:36:56
	* @param page
	* @param dtoList
	* @return
	*/
	public final static Map<String, Object> resultPage(PageInfo page, List dtoList) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("content", dtoList);
		result.put("last", page.getLastPage());
		result.put("totalElements", page.getTotal());
		result.put("totalPages", page.getPages());
		result.put("number", page.getPageNum());
		result.put("size", page.getSize());
		result.put("sort", page.getOrderBy());
		//result.put("numberOfElements", "");
		result.put("first", page.getFirstPage());
		return result;
	}
	
	/**
	 * @author  liuxincheng
	 * @Title: getPage
	 * @Description: TODO(获取page页面中的page信息)
	 * @param @param pageList
	 * @param @return    设定文件
	 * @return PageInfo<T>    返回类型
	 * @throws null
	 */
	public final static <T> PageInfo<T> getPage(Page<T> pageList){
		PageInfo<T> p = new PageInfo<T>();
		
		p.setIsFirstPage(pageList.isFirst());
		p.setIsLastPage(pageList.isLast());
		
		p.setPageNum(pageList.getNumber());
		p.setPages(pageList.getNumberOfElements());
		
		p.setHasNextPage(pageList.hasNext());
		p.setTotal(pageList.getTotalElements());
		
		return p;
	}
}
